<?php
class Operator_lib {
	protected $ci;
	
	public $can_administrate = 0;
	public $can_takeover = 1;
	public $can_viewthreads = 2;
	public $can_modifyprofile = 3;
	
	public $can_count = 4;
	
	protected $permission_ids = array(
			0 => "admin",
			1 => "takeover",
			2 => "viewthreads",
			3 => "modifyprofile"
	);
	
	public function __construct()
	{
		$this->ci =& get_instance();
	}
	function has_online_operators($groupid = "")
	{
		$this->ci->db->select("count(*) as total, min(unix_timestamp(CURRENT_TIMESTAMP)-unix_timestamp(dtmlastvisited)) as time");
		$this->ci->db->from('chatoperator o');
		
		if ($groupid) {
			$this->ci->db->from('chatgroupoperator g');
			$this->ci->db->where('groupid', $groupid);
			$this->ci->db->where('o.operatorid', 'g.operatorid');
		}
		$this->ci->db->where('istatus', 0);
		$row = $this->ci->db->get()->row_array();
		//fixed future
		return true;
		return $row['time'] < $this->ci->config->item('online_timeout', 'chat') && $row['total'] > 0;
	}
	function check_login($redirect = true)
	{
		$operator = $this->ci->session->userdata('operator');
		
		if (empty($operator)) {
			$id = $this->ci->session->userdata('user_id');
			$operator = $this->operator_by_id_($id);
			$this->ci->session->set_userdata(array("operator" => $operator));
		}
		return $operator;
	}
	function is_operator_online($operatorid)
	{
		$this->ci->db->select("count(*) as total, min(unix_timestamp(CURRENT_TIMESTAMP)-unix_timestamp(dtmlastvisited)) as time");
		$this->ci->db->from('chatoperator o');
		
		$this->ci->db->where('operatorid', $operatorid);
		$row = $this->ci->db->get()->row_array();
		//fixed future
// 		return true;
		return $row['time'] < $this->ci->config->item('online_timeout', 'chat') && $row['total'] == 1;
	}
	function operator_by_login($login)
	{
		$this->ci->load->model('chat/operator_model', 'operator_m');
		return $this->ci->operator_m->operator_by_login($login);
	}
	
	function operator_by_id_($id)
	{
		$this->ci->load->model('chat/operator_model', 'operator_m');
		$operator = $this->ci->operator_m->get_array($id);
		if(empty($operator)){
			$this->ci->load->model('users/user_model', 'user_m');
			$user_info = $this->ci->user_m->get(array('id' => $id));
			if(!empty($user_info)){
				$this->ci->operator_m->insert(array(
	    			'operatorid'		=> $user_info->id,
	    			'vclogin'			=> $user_info->username,
	    			'vcpassword'		=> '',
	    			'vclocalename'		=> $user_info->username,
	    			'vccommonname' 		=> $user_info->username,
	    			'vcavatar' 			=> '',
	    			'vcemail' 			=> $user_info->email,
	    			'vcjabbername' 		=> '',
	    			'iperm' 			=> $iperm = '65534'
	    		));
				$operator = $this->ci->operator_m->get_array($id);
			}
		}
		return $operator;
	}
	function get_logged_in()
	{
		$operator = $this->ci->session->userdata("operator");
		return isset($operator) ? $operator : FALSE;
	}
	function operator_is_online($operator)
	{
		return $operator['time'] < $this->ci->config->item('online_timeout', 'chat');
	}
	function operator_is_available($operator)
	{
		return $operator['istatus'] == 0 && $operator['time'] < $this->ci->config->item('online_timeout', 'chat') ? "1" : "";
	}
	
	function operator_is_away($operator)
	{
		return $operator['istatus'] != 0 && $operator['time'] < $this->ci->config->item('online_timeout', 'chat') ? "1" : "";
	}
	
	function operator_get_all()
	{
		$this->ci->load->model('chat/operator_model', 'operator_m');
		return $this->ci->operator_m->operator_get_all();
	}
	
	function is_capable($perm, $operator)
	{
		$permissions = $operator && isset($operator['iperm']) ? $operator['iperm'] : 0;
		return $perm >= 0 && $perm < 32 && ($permissions & (1 << $perm)) != 0;
	}
}
?>